{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import shutil"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "shutil.rmtree(\"outdir\", ignore_errors = True) # start fresh each time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "def decode_csv(row):\n",
    "    cols = tf.decode_csv(row, record_defaults=[[0],['house'],[0]])\n",
    "    features = {'sq_footage': cols[0], 'type': cols[1]}\n",
    "    price = cols[2] # \"label\"\n",
    "    return features, price\n",
    "\n",
    "dataset = tf.data.Dataset.list_files(\"property*\")            \\\n",
    "                         .flat_map(tf.data.TextLineDataset)  \\\n",
    "                         .map(decode_csv)\n",
    "        \n",
    "dataset = dataset.shuffle(1000) \\\n",
    "                 .repeat()  \\\n",
    "                 .batch(3)\n",
    "        \n",
    "featcols = [\n",
    "    tf.feature_column.numeric_column(\"sq_footage\"),\n",
    "    tf.feature_column.indicator_column(\n",
    "        tf.feature_column.categorical_column_with_vocabulary_list(\"type\", [\"house\", \"apt\"])\n",
    "    )\n",
    "]\n",
    "        \n",
    "def train_input_fn():\n",
    "    return dataset.make_one_shot_iterator().get_next()\n",
    "\n",
    "def serving_input_fn():\n",
    "    json = { 'sq_footage' : tf.placeholder(tf.int32, [None]),\n",
    "             'prop_type'  : tf.placeholder(tf.string, [None])\n",
    "           }\n",
    "        \n",
    "    features = { 'sq_footage': json['sq_footage'],\n",
    "                 'type': json['prop_type']\n",
    "               }\n",
    "    return tf.estimator.export.ServingInputReceiver(features, json)\n",
    "\n",
    "run_config = tf.estimator.RunConfig(model_dir=\"outdir\",\n",
    "                                    save_summary_steps=10,\n",
    "                                    save_checkpoints_steps=1000)\n",
    "        \n",
    "export_last = tf.estimator.LatestExporter(\"houseprice\",\n",
    "                                          serving_input_fn)\n",
    "        \n",
    "train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn, max_steps=5000)\n",
    "        \n",
    "eval_spec = tf.estimator.EvalSpec(input_fn=train_input_fn, ## use real evaldata !\n",
    "                                  steps=10, # evals on 100 batches\n",
    "                                  throttle_secs=3, # eval no more than every 3 sec\n",
    "                                  exporters=export_last)\n",
    "\n",
    "#model = tf.estimator.LinearRegressor(featcols, config=run_config)\n",
    "model = tf.estimator.DNNRegressor([3, 2, 2], featcols, config=run_config)\n",
    "\n",
    "tf.estimator.train_and_evaluate(model, train_spec, eval_spec)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# try to read the Dataset by hand\n",
    "features, labels = train_input_fn()\n",
    "with tf.Session() as sess:\n",
    "    for i in range(40):\n",
    "        feat, lab = sess.run([features, labels])\n",
    "        print(\"feat:\" + str(feat) + \" label:\" + str(lab))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
